package com.ruoyi.web.controller.construction.supertube;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.ProductCategory;
import com.ruoyi.system.service.zhaungxiuservice.shopService.ProductCategoryService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Api(value = "超管商品类别接口", tags = {"超管商品类别接口"})
@RestController
@RequestMapping("/super/category")
public class SuperCategoryController extends BaseController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired(required = false)
    private TokenService tokenService;


//    TODO: 这是普通店铺分类的

    //    以下是店铺分类用的
    @PreAuthorize("@ss.hasPermi('super:category:listcommen')")
    @GetMapping("/listcommen/{brandId}")
    public AjaxResult selectProductCategoryList(@PathVariable("brandId") Integer brandId){
//        startPage();
//        List<ProductCategory> list = productCategoryService.selectProductCategoryList(brandId);
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        Long userId1 = loginUser.getUser().getUserId();
//        System.err.println("****************" +userId1);
//        productCategory.setUserId(userId1);
        return AjaxResult.success(productCategoryService.selectProductCategoryList(brandId));
    }

    @PreAuthorize("@ss.hasPermi('super:category:listHouse')")
    @GetMapping("/listHouse")
    public TableDataInfo CategoryListHouse(ProductCategory productCategory){
        startPage();
        List<ProductCategory> list = productCategoryService.CategoryListHouse(productCategory);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('super:category:addcommon')")
    @Log(title = "商品添加分类子级", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addCommonCategory")
    public AjaxResult insertCommonCategory(@RequestBody ProductCategory productCategory)
    {
        return toAjax(productCategoryService.insertCommonCategory(productCategory));
    }
    @PreAuthorize("@ss.hasPermi('super:category:addcommonhouse')")
    @Log(title = "商品添加分类子级", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addHouseCategory")
    public AjaxResult insertCommonHouse(@RequestBody ProductCategory productCategory)
    {
        return toAjax(productCategoryService.insertCommonHouse(productCategory));
    }


    @Log(title = "查询家具主材类别", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('super:category:CommenlistOne')")
    @GetMapping("/findAllCommenOne")
    public List<ProductCategory> findAllCommenOne(){
        return productCategoryService.findAllCommenOne();
    }

    @Log(title = "查询家具材类别", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('super:category:CommenOneoZi')")
    @GetMapping(value = "/findAllCommenOneZi/{parentId}")
    public List<ProductCategory> findAllCommenOneZi(@PathVariable("parentId") Integer parentId){
        return productCategoryService.findAllCommenOneZi(parentId);
    }

    @Log(title = "查询建房主材类别", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('super:category:CommenlistTwo')")
    @GetMapping("/findAllCommenTwo")
    public List<ProductCategory> findAllCommenTwo(){
        return productCategoryService.findAllCommenTwo();
    }

    @Log(title = "查询建房材类别", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('super:category:CommenTwoZi')")
    @GetMapping(value = "/findAllCommenTwoZi/{parentId}")
    public List<ProductCategory> findAllCommenTwoZi(@PathVariable("parentId") Integer parentId){
        return productCategoryService.findAllCommenTwoZi(parentId);
    }

    @PreAuthorize("@ss.hasPermi('super:category:removecommen')")
    @Log(title = "普通商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/deleteCommen/{categoryId}")
    public AjaxResult deleteCommen(@PathVariable Integer[] categoryId)
    {
        return toAjax(productCategoryService.deleteOneSuperByIds(categoryId));
    }


    @PreAuthorize("@ss.hasPermi('super:category:editcommen')")
    @Log(title = "普通商品分类", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/editCommen")
    public AjaxResult editCommen(@RequestBody ProductCategory productCategory)
    {
        return toAjax(productCategoryService.updateCategorySuper(productCategory));
    }


//    ************************************************************************




    /**
     * 获取商品分类详细信息
     */
//    @PreAuthorize("@ss.hasPermi('super:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Integer categoryId)
    {
        return AjaxResult.success(productCategoryService.selectProductCategoryById(categoryId));
    }



    @PreAuthorize("@ss.hasPermi('super:category:remove')")
    @Log(title = "超管商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/deleteCate/{categoryId}")
    public AjaxResult remove(@PathVariable Integer[] categoryId)
    {
        return toAjax(productCategoryService.deleteOneSuperByIds(categoryId));
    }


    @PreAuthorize("@ss.hasPermi('super:category:edit')")
    @Log(title = "超管商品分类", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/editCategory")
    public AjaxResult edit(@RequestBody ProductCategory productCategory)
    {
        return toAjax(productCategoryService.updateCategorySuper(productCategory));
    }



//    TODO: 以下是查询 超管商品分类

    @Log(title = "查询超管建房主材类别", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('super:category:CatelistOne')")
    @GetMapping("/supertfindAllListOne")
    public TableDataInfo supertfindAllListOne(ProductCategory productCategory){
        startPage();
        List<ProductCategory> list = productCategoryService.supertfindAllListOne(productCategory);
        return getDataTable(list);
    }

    @Log(title = "查询超管子级建房主材类别", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('super:category:CatelistOneoZi')")
    @GetMapping(value = "/supertfindAllListOneZi/{parentId}")
    public List<ProductCategory> supertfindAllListOneZi(@PathVariable("parentId") Integer parentId){
//        startPage();
//        List<ProductCategory> list = productCategoryService.supertfindAllListOneZi(parentId);
        return productCategoryService.supertfindAllListOneZi(parentId);
    }





    @Log(title = "查询超管家居建材类别", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('super:category:CatelistTwo')")
    @GetMapping("/supertfindAllListTwo")
    public TableDataInfo supertfindAllListTwo(ProductCategory productCategory){
        startPage();
        List<ProductCategory> list = productCategoryService.supertfindAllListTwo(productCategory);
        return getDataTable(list);
    }

    @Log(title = "查询超管子级家居建材类别", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('super:category:CatelistTwoZi')")
    @GetMapping("/supertfindAllListTwoZi/{parentId}")
    public List<ProductCategory> supertfindAllListTwoZi(@PathVariable("parentId") Integer parentId){
//        startPage();
//        List<ProductCategory> list = productCategoryService.supertfindAllListTwoZi(parentId);
        return productCategoryService.supertfindAllListTwoZi(parentId);
    }

    /**
     * 新增商品分类   父级添加字段 家具建材
     */
    @PreAuthorize("@ss.hasPermi('super:category:add')")
    @Log(title = "商品添加分类父级", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody ProductCategory productCategory)
    {
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i=0;i<8;i++){
            stringBuilder.append(random.nextInt(10));
        }
        String num =stringBuilder.toString();
        productCategory.setCategoryCode(num);
        productCategory.setParentId(0);
        productCategory.setCategoryLevel(1);
        productCategory.setCategoryType("1");
        productCategory.setCategoryStatus("1");
        productCategory.setModifiedTime(new Date());
        productCategory.setDelFlag("0");
        return toAjax(productCategoryService.insertProductCategory(productCategory));
    }

    @PreAuthorize("@ss.hasPermi('super:category:addjf')")
    @Log(title = "商品添加分类父级", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addJF")
    public AjaxResult addJF(@RequestBody ProductCategory productCategory)
    {
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i=0;i<8;i++){
            stringBuilder.append(random.nextInt(10));
        }
        String num =stringBuilder.toString();
        productCategory.setCategoryCode(num);
        productCategory.setParentId(0);
        productCategory.setCategoryLevel(1);
        productCategory.setCategoryType("0");
        productCategory.setCategoryStatus("1");
        productCategory.setModifiedTime(new Date());
        productCategory.setDelFlag("0");
        return toAjax(productCategoryService.insertProductCategory(productCategory));
    }

    @PreAuthorize("@ss.hasPermi('super:category:addZi')")
    @Log(title = "商品添加分类子级", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addZi")
    public AjaxResult addZi(@RequestBody ProductCategory productCategory)
    {
        return toAjax(productCategoryService.insertProductCategoryZi(productCategory));
    }

    @Log(title = "上传类别图标")
    @PostMapping(value = "/uploadImg")
    public String uploadImg(@RequestParam("categoryImg") MultipartFile file) throws IOException {
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
            return avatar;
        }
        return ("上传图片异常，请联系管理员");
    }



}
